﻿/// <reference path="../jquery.min.js" />
/// <reference path="../../data/city.js" />
/// <reference path="../app.js" />

var RssCity = {}
RssCity.Wrap = $('<div class="cityselect"><header>支持中文/拼音/简拼输入<b>X</b><span>重置</span></header><div><ol></ol><table><tbody></tbody></table></div></div>');
$(document.body).append(RssCity.Wrap);
$(document).on("click.rsscity", function () {
    RssCity.Wrap.hide();
});
RssCity.Wrap.click(function (ev) {
    ev.stopPropagation();
    ev.preventDefault();
});
RssCity.Menu = RssCity.Wrap.find("ol");
RssCity.Body = RssCity.Wrap.find("tbody");
RssCity.Wrap.find("b").click(function () {
    RssCity.Wrap.hide();
});
RssCity.Wrap.find("span").click(function () {
    RssCity.Wrap.hide();
    RssCity.onselect("", "");
});
RssCity.onselect = function (key, val) { }
RssCity.onload = [];
RssCity.load = function (host) {
    $.getScript((host || "") + "data/city.js", function () {
        $.each(RssCity.onload, function (idx, val) {
            val();
        });
    })
}
RssCity.Select = function (data) {
    var data = { "r": [] };
    $.each(rydata.city, function (key, rows) {
        var letter = rows[1].substr(0, 1);
        if (!data[letter]) {
            data[letter] = [];
        }
        data[letter].push(key);
        if (rows[2] == "1") {
            data.r.push(key);
        }
    });
    var menu = "<li>热门</li><li>";
    $.each("ABCDEFGHJKLMNPQRSTWXYZ".split(''), function (idx, val) {
        if (idx != 0 && idx % 4 == 0) {
            menu += '</li><li>';
        }
        menu += val;
    });
    menu += '</li>';

    var item = RssCity.Menu.append(menu).find("li");
    item.click(function (ev) {
        ev.stopPropagation();
        ev.preventDefault();
        item.removeClass();
        $(this).addClass("selected");
        var m = $(this).html().replace("热门", "r");
        var html = "";
        $.each(m.split(''), function (idx, val) {
            html += "<tr><td " + (val == "r" ? "class='cityhot'" : "") + ">" + val.replace("r", "") + "</td><td>";
            $.each(data[val], function (idx, key) {
                html += '<a key="' + key + '">' + rydata.city[key][0] + '</a>';
            });
            html += "</td></tr>";
        });
        RssCity.Body.html(html).find("a").click(function () {
            var t = $(this);
            RssCity.onselect(t.attr("key"), t.html());
            RssCity.Wrap.hide();
        });
    }).first().click();
}

$('[ry-city]').click(function (ev) {
    ev.stopPropagation();
    ev.preventDefault();
    var t = $(this), offset = t.offset(), wrap = $("#" + t.attr("ry-city")), text = t.data("text") ? $("#" + t.data("text")) : t;
    wrap.val('');
    text.val('');
    offset.top = offset.top + t.outerHeight() - 1;
    RssCity.onselect = function (key, val) {
        wrap.val(key);
        text.val(val);
    }
    RssCity.Wrap.show().css(offset);
}).keyup(function () {
    var t = $(this), val = (t.data("text") ? $("#" + t.data("text")) : t).val();
    if (!val.length) {
        return;
    }
    var html = '<tr><td class="citysearchitem"><div>';
    $.each(rydata.city, function (key, rows) {
        if (rows[0].indexOf(val) > -1 || rows[1].toLowerCase().indexOf(val) > -1) {
            html += '<a key="' + key + '">' + rows[0].replace(key, "<b>" + key + "</b>") + " " + rows[1].replace(key, "<b>" + key + "</b>") + '</a>';
        }
    });
    html += "</div></td></tr>";
    RssCity.Body.html(html).find("a").click(function () {
        var t = $(this);
        RssCity.onselect(t.attr("key"), t.html().split(' ')[0]);
        RssCity.Wrap.hide();
    });;
});